﻿using EYangFramework.Common.Ninject;
using EYangFramework.Domain.Entitys.ValueObjects;
using SMES.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EYangFramework.Domain.Entitys.Entrys
{
    [Table("EYANG_USER")]
    public class User : Entity
    {
        [StringLength(50)]
        public virtual string USERNAME { get; set; }
        public virtual Gender GENDER { get; set; }

        public virtual User LEADER { get; set; }
        [EmailAddress()]
        [StringLength(50)]
        public virtual string EMAIL { get; set; }
        [StringLength(20)]
        public virtual string PHONE { get; set; }
        public virtual bool ISADMIN { get; set; }
        public virtual bool ISLOCKED { get; set; }
        public virtual bool ISDELETE { get; set; }
        [StringLength(50)]
        public virtual string PASSWORD { get; set; }
        public virtual bool CHANGEPASSWORD { get; set; }
        public virtual Role ROLE { get; set; }//为简化系统,所有用户只能属于一个角色
        public virtual ICollection<Navigation> NAVIGATIONS { get; set; }//用户与导航的关系为多对多
        public virtual ICollection<Menu> MENUS { get; set; }
        public User() { }
        public User(string name, Gender gender, string email, string phone, Role role, User leader, User createUser, bool isAdmin = false, string password = "888")
            :base(createUser)
        {
            this.USERNAME = name;
            this.GENDER = gender;
            this.EMAIL = email;
            this.LEADER = leader;
            this.PHONE = phone;
            this.PASSWORD = CEncoder.encryptMD5(password);
            this.ROLE = role;
            this.ISADMIN = isAdmin;
            this.ISDELETE = false;
            this.ISLOCKED = false;
            this.CHANGEPASSWORD = false;
        }
        public bool IsInRole(Role role)
        {
            return role.USERS.Where(t => t.ID == this.ID).Count() == 1;
        }
        public bool ValidNavigation(Navigation navigation)
        {
            List<Navigation> navigations = NAVIGATIONS.Union(this.ROLE.NAVIGATIONS).ToList();
            return navigations.Any(t => t.ID == navigation.ID);
        }
    }
}
